package com.lw;

import java.awt.*;

/**
 * 底层地图
 * 绘制游戏相关组件
 */
public class MapBottom {

    BottomRay bottomRay = new BottomRay();
    BottomNum bottomNum = new BottomNum();

    /**
     * 重置游戏
     */
    void resetGame(){
        //遍历地图
        for (int i = 1; i <= GameUtil.MAP_W; i++) {
            for (int j = 1; j <= GameUtil.MAP_H; j++) {
                GameUtil.DATA_BOTTOM[i][j] = 0;
            }
        }

        bottomRay.newRay();//生成地雷
        bottomNum.newNum();//标记数字
    }

    /**
     * 绘制方法
     *
     * @param g
     */
    void paintSelf(Graphics g) {
        g.setColor(Color.red);

        //画竖线
        for (int i = 0; i <= GameUtil.MAP_W; i++) {
            g.drawLine(GameUtil.OFFSET + i * GameUtil.SQUARE_LENGTH,
                    3 * GameUtil.OFFSET,
                    GameUtil.OFFSET + i * GameUtil.SQUARE_LENGTH,
                    3 * GameUtil.OFFSET + GameUtil.MAP_H * GameUtil.SQUARE_LENGTH);
        }

        //画竖线
        for (int i = 0; i <= GameUtil.MAP_H; i++) {
            g.drawLine(GameUtil.OFFSET,
                    3 * GameUtil.OFFSET + i * GameUtil.SQUARE_LENGTH,
                    GameUtil.OFFSET + GameUtil.MAP_W * GameUtil.SQUARE_LENGTH,
                    3 * GameUtil.OFFSET + i * GameUtil.SQUARE_LENGTH);

        }

        //遍历地图
        for (int i = 1; i <= GameUtil.MAP_W; i++) {
            for (int j = 1; j <= GameUtil.MAP_H; j++) {
                if (GameUtil.DATA_BOTTOM[i][j] == -1) {//画雷
                    //x+1,y+1,width-2,height-2 都是为了让图片不遮挡格子线条
                    g.drawImage(GameUtil.lei,
                            GameUtil.OFFSET + (i - 1) * GameUtil.SQUARE_LENGTH + 1,
                            GameUtil.OFFSET * 3 + (j - 1) * GameUtil.SQUARE_LENGTH + 1,
                            GameUtil.SQUARE_LENGTH - 2,
                            GameUtil.SQUARE_LENGTH - 2,
                            null);
                } else if (GameUtil.DATA_BOTTOM[i][j] >= 0) {//画数字
                    if(GameUtil.DATA_BOTTOM[i][j]>8){
                        System.out.println("画数字："+i+","+j+" "+GameUtil.DATA_BOTTOM[i][j]);
                    }else{
                        g.drawImage(GameUtil.images[GameUtil.DATA_BOTTOM[i][j]],
                                GameUtil.OFFSET + (i - 1) * GameUtil.SQUARE_LENGTH + 1,
                                GameUtil.OFFSET * 3 + (j - 1) * GameUtil.SQUARE_LENGTH + 1,
                                GameUtil.SQUARE_LENGTH - 2,
                                GameUtil.SQUARE_LENGTH - 2,
                                null);
                    }
                }

            }
        }

        //绘制数字，剩余雷数
        GameUtil.drawWord(g,"剩余雷："+(GameUtil.RAY_MAX-GameUtil.FLAG_NUM)
                ,GameUtil.OFFSET,2*GameUtil.OFFSET,
                30,Color.red);

        GameUtil.drawWord(g,"耗时："+((GameUtil.END_TIME-GameUtil.START_TIME)/1000)
                ,GameUtil.SQUARE_LENGTH*(GameUtil.MAP_W)-2*GameUtil.OFFSET,2*GameUtil.OFFSET,
                30,Color.red);

        //游戏状态绘制
        switch (GameUtil.state){
            case 0->{
                GameUtil.END_TIME = System.currentTimeMillis();
                g.drawImage(GameUtil.face,
                        GameUtil.OFFSET+GameUtil.MAP_W/2*GameUtil.SQUARE_LENGTH,
                        GameUtil.OFFSET,
                        null);
            }
            case 1->g.drawImage(GameUtil.win,
                    GameUtil.OFFSET+GameUtil.MAP_W/2*GameUtil.SQUARE_LENGTH,
                    GameUtil.OFFSET,
                    null);
            case 2->g.drawImage(GameUtil.over,
                    GameUtil.OFFSET+GameUtil.MAP_W/2*GameUtil.SQUARE_LENGTH,
                    GameUtil.OFFSET,
                    null);
        }
    }
}
